package com.example.fengshoutong.ui;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.fengshoutong.R;

import java.util.ArrayList;
import java.util.List;

public class AddressManagerActivity extends AppCompatActivity {

    private ListView addressListView;
    private Button addAddressButton;
    private List<String> addresses = new ArrayList<>();
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);

        addressListView = findViewById(R.id.address_list_view);
        addAddressButton = findViewById(R.id.add_address_button);

        // 初始化数据源
        addresses.add("河南省郑州市XXXX街道");
        addresses.add("吉林省长春市XXXX街道");

        // 设置适配器
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, addresses);
        addressListView.setAdapter(adapter);

        // 添加地址按钮点击事件
        Button btnAddAddress = findViewById(R.id.add_address_button);
        btnAddAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddAddressDialog();
            }
        });

        // 列表项点击事件
        addressListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedAddress = (String) parent.getItemAtPosition(position);
                // 在这里处理选中地址的逻辑
            }
        });
    }
    private void showAddAddressDialog() {
        final EditText input = new EditText(this);
        input.setText("请输入地址");
        input.setInputType(InputType.TYPE_CLASS_TEXT);

        new AlertDialog.Builder(this)
                .setTitle("添加地址")
                .setView(input)
                .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String address = input.getText().toString().trim();
                        if (!address.isEmpty()) {
                            addresses.add(address);
                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(AddressManagerActivity.this, "地址不能为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
}